package cn.io0288.blog2;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LogoutServlet", value = "/logout")
public class LogoutServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        HttpSession session = request.getSession();
        String[] route = request.getRequestURI().split("/");

        // 是否登录
        if (session.getAttribute("uemail") == null) {
            response.sendRedirect(request.getContextPath() + "/login");
            return;
        }

        session.removeAttribute("uemail");
        session.removeAttribute("uname");
        System.out.println("登出成功, 200");
        request.setAttribute("msg", "登出成功");
        response.sendRedirect(request.getContextPath() + "/");
    }
}